package cn.danger.com.fragment.memo;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Map;

import cn.danger.com.activity.CommonActivity;
import cn.danger.com.activity.R;
import cn.danger.com.fragment.BackHandledFragment;
import cn.danger.com.model.entity.Memo;
import cn.danger.com.presenter.MemoPresenter;
import cn.danger.com.presenter.MemoPresenterImpl;
import cn.danger.com.util.Constants;
import cn.danger.com.util.FileCacheService;
import cn.danger.com.view.CommonView;

/**
 * Created by 俊 on 2016/12/31.
 */
public class AddMemoFragment extends BackHandledFragment implements CommonView {

    private boolean hadIntercept;
    private View view;
    private CommonActivity activity;
    private Toolbar toolbar;
    private TextView toolbar_title;
    private EditText memo_title_editText;
    private EditText memo_content_editText;
    private ProgressDialog dialog;
    private MemoPresenter presenter;
    private Bundle bundle;
    private Memo memo;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_add_memo, container, false);

        bindView();
        bindEvent();

        return view;
    }

    @Override
    public void bindView() {
        activity = (CommonActivity) getActivity();
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        toolbar.setTitle("");
        activity.setSupportActionBar(toolbar);

        toolbar_title = (TextView) view.findViewById(R.id.toolbar_title);
        memo_title_editText = (EditText) view.findViewById(R.id.memo_title_editText);
        memo_content_editText = (EditText) view.findViewById(R.id.memo_content_editText);

        dialog = new ProgressDialog(activity, ProgressDialog.THEME_HOLO_LIGHT);
        dialog.setMessage("正在执行...");
        dialog.setCanceledOnTouchOutside(false);

        presenter = new MemoPresenterImpl(activity, this);

        bundle = getArguments();

        if (bundle != null) {
            memo = (Memo) bundle.getSerializable("memo");
            if (memo != null) {
                toolbar_title.setText(getResources().getString(R.string.update_memo));
                memo_title_editText.setText(memo.getTitle());
                memo_content_editText.setText(memo.getContent());
                Log.i("danger", "objectId---->>>" + memo.getObjectId());
            }
        }
    }

    @Override
    public void bindEvent() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bundle != null) {
                    getFragmentManager().popBackStack();
                } else {
                    activity.finish();
                }
            }
        });
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.modify_menu, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.save) {
            if (memo != null) {
                updateMemo();
            } else {
                saveMemo();
            }
        }
        return super.onOptionsItemSelected(item);
    }

    private void updateMemo() {
        memo.setTitle(memo_title_editText.getText().toString());
        memo.setContent(memo_content_editText.getText().toString());
        FileCacheService service = new FileCacheService(activity);
        Map<String, String> map = (Map<String, String>) service.getSharePreference("userInfo");
        if (map.get("phone") == null) {
            memo.setUsername("default");
        } else {
            memo.setUsername(map.get("phone"));
        }
        presenter.updateMemo(memo);
    }

    private void saveMemo() {
        Memo new_memo = new Memo();
        new_memo.setTitle(memo_title_editText.getText().toString());
        new_memo.setContent(memo_content_editText.getText().toString());
        FileCacheService service = new FileCacheService(activity);
        Map<String, String> map = (Map<String, String>) service.getSharePreference("userInfo");
        if (map.get("phone") == null) {
            new_memo.setUsername("default");
        } else {
            new_memo.setUsername(map.get("phone"));
        }
        presenter.addMemo(new_memo);
    }

    @Override
    public void showDialog() {
        dialog.show();
    }

    @Override
    public void hideDialog() {
        dialog.dismiss();
    }

    @Override
    public void showMessage(String message) {
        Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
        Intent intent = new Intent("update_memo_list");
        if (message.equals(Constants.SUCCESS_TO_ADD_MEMO) || message.equals(Constants.SUCCESS_TO_SAVE_LOCAL)) {
            activity.sendBroadcast(intent);
            if (bundle != null) {
                getFragmentManager().popBackStack();
            } else {
                activity.finish();
            }
        }
        if (message.equals(Constants.SUCCESS_TO_UPDATE_LOCAL) || message.equals(Constants.SUCCESS_TO_UPDATE_MEMO)) {
            activity.sendBroadcast(intent);
            activity.finish();
        }
    }

    @Override
    public boolean onBackPressed() {
        if (hadIntercept) {
            return false;
        } else {
            hadIntercept = true;
            return true;
        }
    }

}